package reflect;

import java.util.Scanner;

/**
 * 使用反射机制实例化对象
 */
public class ReflectDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Person p = new Person();
        System.out.println(p);


        //1.加载需要实例化对象的类的类对象
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        Class cls = Class.forName(name);
        //2.类对象提供了方法:newInstance()可以利用其表示的类的公开的无参构造器实例化
        Object obj = cls.newInstance();
        System.out.println(obj);
    }
}
